package cn.tang.tframe.common.base;

import cn.tang.tframe.common.utils.DateUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.Map;


public class PageQuery extends BaseQuery {
	private static final long serialVersionUID = 1L;

	public String toSearchString() {
		StringBuffer sbuf = new StringBuffer();
		Field[] fields = this.getClass().getDeclaredFields();
		if (null != fields) {
			for (Field field : fields) {
				Object value = null;
				try {
					if (Modifier.isStatic(field.getModifiers())) {
						continue;
					}
					field.setAccessible(true);
					value = field.get(this);
					if (field.getType().isAssignableFrom(Date.class)) {
						value = DateUtils.format((Date) value, "yyyy-MM-dd HH:mm:ss");
					}
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
				if (null != value) {
					sbuf.append("&");
					sbuf.append(field.getName());
					sbuf.append("=");
					sbuf.append(value);
				}
			}
		}
		sbuf.append("&pageSize=");
		sbuf.append(pageSize);
		return sbuf.toString();
	}
	public static String toSearchString(Map<String,Object> params){
		StringBuffer sbuf = new StringBuffer();
		if(null != params){
			for(Map.Entry<String,Object> field:params.entrySet()){
				Object value = field.getValue();
				if(value instanceof  Date ){
					value = DateUtils.format((Date) value, "yyyy-MM-dd HH:mm:ss");
				}
				if(null != value){
					sbuf.append("&");
					sbuf.append(field.getKey());
					sbuf.append("=");
					sbuf.append(value);
				}
			}
		}
		return sbuf.toString();
	}
}